<template>
  <el-form ref="form" label-position="right" :model="currentRow" :rules="rules" label-suffix="："
           label-width="100px">

    <el-row>
      <el-col :span="8">
        <el-form-item label="隐患点名称" prop="damageName">
          <el-input v-model="currentRow.damageName" placeholder="输入隐患点名称"></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="8">
        <el-form-item label="隐患等级" prop="dangerLevel">
          <el-select v-model="currentRow.dangerLevel" clearable placeholder="选择隐患等级">
            <el-option
              v-for="item in EnumDangerLevelList"
              :key="item.code"
              :label="item.name"
              :value="item.code">
            </el-option>
          </el-select>
        </el-form-item>
      </el-col>
      <el-col :span="8">
        <el-form-item label="排查时间" prop="investigateTime">
          <el-date-picker
            v-model="currentRow.investigateTime"
            type="datetime"
            value-format="timestamp"
            placeholder="选择排查时间">
          </el-date-picker>
        </el-form-item>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="8">
        <el-form-item label="路线" prop="roadId">
          <el-select v-model="currentRow.roadId" clearable placeholder="选择路线" @change="changeRoad">
            <el-option
              v-for="item in roadList2"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="行政区域" prop="region">
          <region-unit v-model="currentRow.region" :key="currentRow.region"></region-unit>
        </el-form-item>
        <el-form-item label="排查单位" >
          <el-input v-model="currentRow.unitName"  placeholder="输入排查单位名称"></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="8">
        <el-form-item label="标段" prop="roadSectionId">
          <el-select v-model="currentRow.roadSectionId" clearable placeholder="选择标段" @change="changeRoadSection">
            <el-option
              v-for="item in sectionList"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="起点桩号" prop="startPointStakeNum">
          <el-input v-model="currentRow.startPointStakeNum" type="number" placeholder="80.500表示K80+500"></el-input>
        </el-form-item>
        <el-form-item label="排查人" >
          <el-input v-model="currentRow.userName"  placeholder="排查人员姓名"></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="8">
        <el-form-item label="排查类型" prop="investigateType">
          <el-select v-model="currentRow.investigateType" clearable placeholder="选择排查类型">
            <el-option
              v-for="item in EnumInvestigateTypeList"
              :key="item.code"
              :label="item.name"
              :value="item.code">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="终点桩号" prop="endPointStakeNum">
          <el-input v-model="currentRow.endPointStakeNum"  type="number" placeholder="80.500表示K80+500"></el-input>
        </el-form-item>
        <el-form-item label="联系电话" prop="userPhone">
          <el-input v-model="currentRow.userPhone" placeholder="排查人员电话"></el-input>
        </el-form-item>
      </el-col>
    </el-row>
    <!-- 工点信息 -->
    <collapse-wrap title="现场照片">
      <el-tooltip v-if="currentRow.picList.length >= 15" class="item" effect="dark" content="最多上传15张图片"
                  placement="top-start">
        <el-button class=" is-disabled" size="small" type="primary">点击上传</el-button>
      </el-tooltip>
      <upload-img
        v-else
        trigger="button"
        :size-limit="5"
        @update="setFormImg"></upload-img>
      <el-form :model="currentRow" ref="imgForm" class="imgFrom" :rules="imgFormRules" size="small">
        <el-table border :data="currentRow.picList" class="m-t-24">
          <el-table-column label="图片名称">
            <template slot-scope="scope">
              <el-form-item :prop="`picList.${scope.$index}.name`" :rules="imgFormRules.name">
                <el-input v-model="scope.row.name" placeholder="请输入图片名称" :maxlength="20" style="width:100%"></el-input>
              </el-form-item>
            </template>
          </el-table-column>
          <el-table-column label="描述">
            <template slot-scope="scope">
              <el-form-item :prop="`picList.${scope.$index}.info`">
                <el-input v-model="scope.row.info" placeholder="请输入图片描述" :maxlength="20" style="width:100%"></el-input>
              </el-form-item>
            </template>
          </el-table-column>
          <el-table-column label="图片" width="150">
            <template slot-scope="scope">
              <el-image style="width: 100px; height: 100px" fit="scale-down" :src="constUrl + scope.row.url"></el-image>
            </template>
          </el-table-column>

          <el-table-column label="操作" width="90">
            <template slot-scope="scope">
              <el-button type="text" @click="handleDeleteImg(scope.$index)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-form>
    </collapse-wrap>

  </el-form>
</template>
<script>
import CollapseWrap from '@/components/collapse-wrap'
import ItemMixins from "./item.mixins";
import constUrl from '@/services/env'
import UploadImg from '@/components/upload-img'
import RegionUnit from '@/components/region-unit2'
import Enums from '@/utils/enums'

export default {
  props: ['value', 'roadList2'],
  components: {UploadImg, CollapseWrap, RegionUnit},
  mixins: [ItemMixins,Enums],
  data() {
    return {
      formSelectSlopeList: [],
      EnumDangerLevelList: [],
      EnumInvestigateTypeList: [],
      sectionList: [],
      // 单位信息表单 校验规则
      rules: {
        damageName: {required: true, message: '请输入隐患点名称'},
        dangerLevel: {required: true, message: '请选择隐患等级'},
        region: {required: true, message: '请选择行政区域'},
        roadId: {required: true, message: '请选择路线'},
        highwayGrade: {required: true, message: '请选择公路等级'},
        unitName: {required: true, message: '请输入排查单位'},
        userName: {required: true, message: '请输入排查人姓名'},
        userPhone: {required: true, message: '请输入排查人电话'},
        roadSectionId: {required: true, message: '请选择标段'},
        slopeId: {required: true, message: '请选择边坡'},
        investigateTime: {required: true, message: '请选择排查时间'},
        investigateType: {required: true, message: '请选择排查类型'},
      },
      imgFormRules: {
        name: {required: true, message: '请输入图片名称'},
        shotTime: {required: true, message: '请选择拍摄时间'},
      },
      constUrl,
    }
  },
  computed: {
    currentRow: {
      get() {
        return this.value
      },
      set(value) {
        this.$emit('input', value)
      }
    }
  },
  methods: {
    // 删除图片
    handleDeleteImg(index) {
      this.currentRow.picList.splice(index, 1)
    },
    // 上传成功后处理图片
    setFormImg(path) {
      let item = {
        name: '',
        shotTime: '',
        url: path
      }
      this.currentRow.picList.push(item)
    },
    //选择路线
    changeRoad() {
      this.$set(this.currentRow, 'roadSectionId', '');
      this.sectionList = []
      const {roadId} = this.currentRow
      this.getBidSectionList(roadId)
    },
    changeRoadSection() {
      this.$set(this.currentRow, 'slopeId', '');
      this.slopeList = []
      const {roadSectionId} = this.currentRow
      this.getSelectSlopeList(roadSectionId)

    },
    // 内部表单校验
    _validatorForm() {
      return new Promise((resolve) => {
        this.$refs.form.validate((valid) => {
          valid && resolve()
        })
      })
    }
  },
  mounted() {
    this.queryDictList(['EnumDangerLevel', 'EnumInvestigateType'],
      ['EnumDangerLevelList', 'EnumInvestigateTypeList']);
    if (this.currentRow.roadId) {
      this.getBidSectionList(this.currentRow.roadId)
    }
  },
  created() {
  },
  activated() {
    this.$nextTick(() =>
      this.$refs.form.clearValidate()
    )
  }
}
</script>

<style lang="less">
  .el-date-editor.el-input, .el-date-editor.el-input__inner {
    width: 298px !important;
  }

  .form_compact {
    display: flex;

    & /deep/ .el-input input {
      border-top-right-radius: 0;
      border-bottom-right-radius: 0;
    }

    & /deep/ .el-button {
      border-top-left-radius: 0;
      border-bottom-left-radius: 0;
    }
  }

  .el-button {
    padding-right: 40px;
  }

  .el-form-item__label {
    /*padding: 0px;*/
  }

  input::-webkit-inner-spin-button {
    -webkit-appearance: none !important;
  }
</style>
